package cn.bugDance.domain.strategy.service.rule.tree.impl;

import cn.bugDance.domain.activity.model.entity.ActivityDayAccountEntity;
import cn.bugDance.domain.activity.repository.IActivityRepository;
import cn.bugDance.domain.strategy.model.entity.RuleConfigEntity;
import cn.bugDance.domain.strategy.model.entity.RuleTreeDetermineEntity;
import cn.bugDance.domain.strategy.model.entity.RuleTreeFactorEntity;
import cn.bugDance.domain.strategy.model.valobj.DetermineTypeVO;
import cn.bugDance.domain.strategy.model.valobj.RuleTreeLineVO;
import cn.bugDance.domain.strategy.model.valobj.StrategyConstant;
import cn.bugDance.domain.strategy.repository.IStrategyRepository;
import cn.bugDance.domain.strategy.service.rule.tree.AbstractRuleTree;
import cn.bugDance.domain.strategy.service.rule.tree.factory.RuleTreeFactory;
import cn.bugDance.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Component(StrategyConstant.StrategyRule.RULE_LOCK)
@Slf4j
public class RuleLockTree extends AbstractRuleTree {

    @Resource
    private IStrategyRepository strategyRepository;
    @Resource
    private IActivityRepository activityRepository;


    @Override
    public RuleTreeDetermineEntity action(RuleTreeFactorEntity ruleTreeFactorEntity) {

        log.info("【策略领域层-RuleLockTree】: 规则规则树引擎节点-次数锁执行, strategyId: {}, awardId: {}", ruleTreeFactorEntity.getStrategyId(), ruleTreeFactorEntity.getAwardId());

        Long strategyId = ruleTreeFactorEntity.getStrategyId();
        Integer awardId = ruleTreeFactorEntity.getAwardId();
        List<String> ruleModels = ruleTreeFactorEntity.getRuleModels();
        Long activityId = ruleTreeFactorEntity.getActivityId();
        String day = ruleTreeFactorEntity.getDay();
        String userId = ruleTreeFactorEntity.getUserId();

        if(!ruleModels.contains(StrategyConstant.StrategyRule.RULE_LOCK)){
            RuleTreeDetermineEntity ruleTreeDetermineEntity = new RuleTreeDetermineEntity();
            ruleTreeDetermineEntity.setDetermineTypeVO(DetermineTypeVO.ALLOW);
            ruleTreeDetermineEntity.setAwardId(awardId);
            return ruleTreeDetermineEntity;
        }

        RuleConfigEntity ruleConfigEntity = strategyRepository.queryRuleConfigRuleAward(strategyId, awardId, StrategyConstant.StrategyRule.RULE_LOCK);
        Integer count = Integer.valueOf(ruleConfigEntity.getConfig());

        ActivityDayAccountEntity activityDayAccountEntity = activityRepository.queryActivityDayAccountEntity(userId, activityId, day);
        Integer userCount = 0;
        if(activityDayAccountEntity != null){
            userCount = activityDayAccountEntity.getDayCount() - activityDayAccountEntity.getDayCountSurplus();
        }
        if(userCount >= count){
            RuleTreeDetermineEntity ruleTreeDetermineEntity = new RuleTreeDetermineEntity();
            ruleTreeDetermineEntity.setDetermineTypeVO(DetermineTypeVO.ALLOW);
            ruleTreeDetermineEntity.setAwardId(awardId);
            return ruleTreeDetermineEntity;
        }

        RuleTreeDetermineEntity ruleTreeDetermineEntity = new RuleTreeDetermineEntity();
        ruleTreeDetermineEntity.setDetermineTypeVO(DetermineTypeVO.TAKE_OVER);
        ruleTreeDetermineEntity.setAwardId(awardId);
        return ruleTreeDetermineEntity;
    }
}
